package raytracer.geometry;

public class Scale extends Transformation {

	/** scaling factor. */
	private final float s;
	/** origin of scaling. */
	private final float[] center;

	/**
	 * Scalingmatrix with scale s and origin in center.
	 * 
	 * @param sf scaling factor
	 * @param center origin of scaling
	 */
	public Scale(float sf, float[] center) {
		super();

		this.s = sf;
		this.center = center;

		matrix[0] = new float[] { sf, 0, 0, 0 };
		matrix[1] = new float[] { 0, sf, 0, 0 };
		matrix[2] = new float[] { 0, 0, sf, 0 };
		matrix[3] = new float[] { 0, 0, 0, 1 };

		Translation trans = new Translation(center);
		// M = T * S * T^-1
		Transformation T1 = trans.inverse();
		Transformation ST1 = this.combine(T1);
		Transformation TST1 = trans.combine(ST1);
		this.matrix = TST1.matrix;
	}

	@Override
	public Transformation inverse() {
		return new Scale(s == 0 ? 0 : 1 / s, center);
	}
}
